// 导出blob为Excel文件
export const exportExcelFile = (data, res, name = '') => {
  if (!data) {
    return;
  }

  // 获取headers中的fileName名
  const tempName =
    res.headers['content-disposition'] && res.headers['content-disposition'].split(';')[1].split('filename=')[1];
  // iconv-lite解决中文乱码
  const iconv = require('iconv-lite');
  iconv.skipDecodeWarning = true; // 忽略警告
  const fileName = name || iconv.decode(tempName, 'gbk');
  const blob = new Blob([data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
  }); // 转成二进制对象
  if ('download' in document.createElement('a')) {
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', decodeURIComponent(fileName));
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
  } else {
    window.navigator.msSaveBlob(blob, decodeURIComponent(fileName));
  }
};
